/**
 * @author: Pegasus  Date: 2020-08-29
 *
 * 同住客人
 */

import React, {Component} from 'react';
import {
    SafeAreaView,
    View,
    Text,
    Image,
    TouchableOpacity,
    ScrollView,
    Modal, Platform, BackHandler, ToastAndroid
} from 'react-native';
import {connect} from 'react-redux';
import {COLOR_GRAY_BACKGROUND, COLOR_WHITE, FONT_COLOR_SUB_TITLE,FONT_COLOR_TITLE} from "../../../constant/PHColors"
import {styles} from './RoommatesScreen.Style'
import {FetchStatus} from "../../../configuration/RequestConfiguration";
import {ReturnStatus} from "../../../configuration/ReturnStatus";
import FontAwesome from "react-native-vector-icons/FontAwesome";
import AntDesign from "react-native-vector-icons/AntDesign";
import FontAwesome5 from "react-native-vector-icons/FontAwesome5";
import MaterialCommunityIcons from "react-native-vector-icons/MaterialCommunityIcons";
import MaterialIcons from "react-native-vector-icons/MaterialIcons";
import BottomButtonView_room from "../../../component/BottomButtonView/BottomButtonView_room";
import BottomButtonList from "../../../component/BottomButtonView/BottomButtonList";
import {doDisplayWarnMessage} from "../../../component/Message/Action";
import {retrieveRoommates} from "./Actions";
import LoadingView from "../../../component/LoadingView/LoadingView";
import {Action} from "../../../configuration/ActionCreator";
import OffWorkView from "../../../component/OffWorkView/OffWorkView";
import {SCREEN_WIDTH} from "../../../constant/PHScreen";

class RoommatesScreen extends Component {

    static navigationOptions = ({navigation}) => ({
        headerTitle: navigation.getParam('title_left'),
        headerTitleStyle: {
            flex:1,
            flexDirection:'row',
            textAlign: 'center'
        },
    });

    // 构造
    constructor(props) {
        super(props);

        // 初始状态
        this.state = {
            buttonList:{},
            menu:[],
            form:[],
            room:this.props.navigation.getParam("room"),                                //房号
            orderid:this.props.navigation.getParam("orderid")?this.props.navigation.getParam("orderid"):'',                         //orderid(暂时的)
            qty:this.props.navigation.getParam("qty")?this.props.navigation.getParam("qty"):'0',                         //qty(暂时的)
            procedure:"WHSKRetrieveRoomMsg",
        };
    }

    componentWillReceiveProps(nextProps, nextContext): void {
        let {dispatch} = this.props;

        if (nextProps.onRetrieveRoommates !== this.props.onRetrieveRoommates && nextProps.onRetrieveRoommates.fetchStatus === FetchStatus.Success) {
            if (nextProps.onRetrieveRoommates.RETURN === ReturnStatus.Success) {
                let title = JSON.stringify(nextProps.onRetrieveRoommates.title);
                this.getTitle(title);

                this.setState({
                    menu:nextProps.onRetrieveRoommates.menu,
                    form:nextProps.onRetrieveRoommates.form,
                })

            } else {
                dispatch(doDisplayWarnMessage(nextProps.onRetrieveRoommates.MSG))
            }
        }
    }

    // 组件渲染后调用
    componentDidMount() {
        let {dispatch} = this.props;

        let str = {"arg":{"orderid":this.state.orderid,"room":this.state.room}}
        dispatch(retrieveRoommates(JSON.stringify(str),this.state.procedure))
    }

    render() {
        let {onRetrieveRoommates} = this.props;
        let _this = this;
        if (onRetrieveRoommates.fetchStatus === FetchStatus.Success) {
            return (
                <SafeAreaView style={styles.body}>
                    <ScrollView style={styles.body}>

                        {
                            (this.state.form.length>0)&&(
                                <View style={{marginBottom:10,marginTop:10,marginLeft: 5,marginRight: 5,}}>
                                    {
                                        this.showInfo(this.state.form)
                                    }
                                </View>
                            )
                        }

                        {
                            (this.state.menu.length>0)&&(
                                <View style={{marginBottom:10,marginLeft: 5,marginRight: 5,}}>
                                    {
                                        this.showInfo(this.state.menu)
                                    }
                                </View>
                            )
                        }
                    </ScrollView>
                </SafeAreaView>
            )
        }else{
            return <LoadingView/>
        }

    }

    showInfo(datasource){
        let _this = this;
        let info_html = [];

        datasource.map((item, index)=>{
            switch (item.name) {
                case '空行':
                    let line_html = (
                        <View key={index} style={{height:10,backgroundColor:'#FFF'}}>
                        </View>
                    );
                    info_html.push(line_html);
                    break;
                case '分割线':
                    let separate_line_html = (
                        <View key={index} style={{height:2,backgroundColor:'#E5ECED'}}>
                        </View>
                    );
                    info_html.push(separate_line_html);
                    break;
                case '分割':
                    let separate_html = (
                        <View key={index} style={{height:10,backgroundColor:COLOR_GRAY_BACKGROUND}}>
                        </View>
                    );
                    info_html.push(separate_html);
                    break;
                case '标题':
                    let title_html = (
                        <View key={index}>
                            <View style={[styles.guestInfo,{marginBottom:0}]}>
                                <View style={styles.textBox}>
                                    <View style={styles.leftBox}>
                                        <Text style={styles.title_text}>
                                            {item.title}
                                        </Text>
                                    </View>
                                </View>
                            </View>
                        </View>
                    );
                    info_html.push(title_html);
                    break;
                case '明细':
                    let color = item['tcolor']?item.tcolor:FONT_COLOR_SUB_TITLE;
                    let detail_html = (
                        <View key={index} style={[styles.guestInfo,{marginBottom:0}]}>
                            <View style={styles.textBox}>
                                <View style={styles.leftBox}>
                                    <Text style={styles.infoName}>
                                        {item.title}
                                    </Text>
                                    <Text style={[styles.infoText,{color:color}]}>
                                        {item.value}
                                    </Text>
                                </View>
                            </View>
                        </View>
                    );
                    info_html.push(detail_html);
                    break;
                case '同住链接':
                    let share_link_html = (
                        <TouchableOpacity activeOpacity={0.8} onPress={()=>{
                            _this.props.navigation.navigate('RoommatesScreen',{'gno':item.gno});
                        }}>
                            {
                                this.getLinkHtml(item,index)
                            }
                        </TouchableOpacity>
                    );
                    info_html.push(share_link_html);
                    break;
                case '客账链接':
                    let ledger_link_html = (
                        <TouchableOpacity activeOpacity={0.8} onPress={()=>{

                        }}>
                            {
                                this.getLinkHtml(item,index)
                            }
                        </TouchableOpacity>
                    );
                    info_html.push(ledger_link_html);
                    break;
                case '客人':
                    let guest_html = (
                        <View key={index}>
                            {
                                JSON.parse(item.info).map((item1, index1) => {
                                    return(
                                        <View style={{marginTop:10,marginLeft: 10,marginRight: 10,borderWidth: 2,borderColor:'#dbdbdb',borderRadius:5,}}>
                                            {
                                                this.render_guestInfo(item1, index1)
                                            }
                                        </View>
                                    )
                                })
                            }
                        </View>
                    );
                    info_html.push(guest_html);
                    break;
                case '清洁':
                    let clean_html = (
                        <TouchableOpacity activeOpacity={0.8} onPress={()=>{
                            _this.props.navigation.navigate('RoomStatusScreen',{room:_this.state.room,button:'yes',authority:'清洁人',orderid:item.orderid,callBack:()=>_this.freshScreen()});
                        }}>
                            {
                                this.getLinkHtml(item,index)
                            }
                        </TouchableOpacity>
                    );
                    info_html.push(clean_html);
                    break;
                case '检查':
                    let check_html = (
                        <TouchableOpacity activeOpacity={0.8} onPress={()=>{
                            _this.props.navigation.navigate('RoomStatusScreen',{room:_this.state.room,button:'yes',authority:'检查人',orderid:item.orderid,callBack:()=>_this.freshScreen()});
                        }}>
                            {
                                this.getLinkHtml(item,index)
                            }
                        </TouchableOpacity>
                    );
                    info_html.push(check_html);
                    break;
                case '查房':
                    let CheckUnusual_html = (
                        <TouchableOpacity activeOpacity={0.8} onPress={()=>{
                            if(item.iconr){
                                this.props.navigation.navigate('ShowOrderDetailScreen',{room:_this.state.room,ordertype:'查房,查遗留/异常/消费',callBack:()=>_this.backScreen() });
                            }else{
                                this.props.navigation.navigate('addCheckUnusualScreen',{place:_this.state.room,gno:this.state.form[0].gno,callBack:()=>_this.backScreen()});
                            }
                        }}>
                            {
                                this.getLinkHtml(item,index)
                            }
                        </TouchableOpacity>
                    );
                    info_html.push(CheckUnusual_html);
                    break;
                case '布草':
                    let LinenAndBar_html = (
                        <TouchableOpacity activeOpacity={0.8} onPress={()=>{
                            if(item.iconr){
                                this.props.navigation.navigate('ShowOrderDetailScreen',{room:_this.state.room,ordertype:'布草/易耗品/酒水补充',callBack:()=>_this.backScreen() });
                            }else{
                                this.props.navigation.navigate('addLinenAndBarScreen',{place:_this.state.room,gno:this.state.form[0].gno,callBack:()=>_this.backScreen()});
                            }
                        }}>
                            {
                                this.getLinkHtml(item,index)
                            }
                        </TouchableOpacity>
                    );
                    info_html.push(LinenAndBar_html);
                    break;
                case '新建清洁工单':
                    let new_clean_html = (
                        <TouchableOpacity activeOpacity={0.8} onPress={()=>{
                            this.props.navigation.navigate('newClean',{room:_this.state.room,qty:_this.state.qty,callBack:()=>_this.backScreen()});
                        }}>
                            {
                                this.getLinkHtml(item,index)
                            }
                        </TouchableOpacity>
                    );
                    info_html.push(new_clean_html);
                    break;
                case '检查检查申请':
                    let new_check_html = (
                        <TouchableOpacity activeOpacity={0.8} onPress={()=>{

                        }}>
                            {
                                this.getLinkHtml(item,index)
                            }
                        </TouchableOpacity>
                    );
                    info_html.push(new_check_html);
                    break;
                case '新建赶房工单':
                    let new_scurry_html = (
                        <TouchableOpacity activeOpacity={0.8} onPress={()=>{
                            this.props.navigation.navigate('newScurry',{room:_this.state.room,callBack:()=>_this.backScreen()});
                        }}>
                            {
                                this.getLinkHtml(item,index)
                            }
                        </TouchableOpacity>
                    );
                    info_html.push(new_scurry_html);
                    break;
                case '新建退房检查':
                    let check_out_html = (
                        <TouchableOpacity activeOpacity={0.8} onPress={()=>{
                            this.props.navigation.navigate('newCheckOut',{room:_this.state.room,callBack:()=>_this.backScreen()});
                        }}>
                            {
                                this.getLinkHtml(item,index)
                            }
                        </TouchableOpacity>
                    );
                    info_html.push(check_out_html);
                    break;
                case '物品/服务':
                    let goodService_html = (
                        <TouchableOpacity activeOpacity={0.8} onPress={()=>{
                            this.props.navigation.navigate('newGoodService',{room:_this.state.room,callBack:()=>_this.backScreen()});
                        }}>
                            {
                                this.getLinkHtml(item,index)
                            }
                        </TouchableOpacity>
                    );
                    info_html.push(goodService_html);
                    break;
                case '住房打扫':
                    let roomClean_html = (
                        <TouchableOpacity activeOpacity={0.8} onPress={()=>{
                            this.props.navigation.navigate('newRoomCleanScreen',{room:_this.state.room,callBack:()=>_this.backScreen()});
                        }}>
                            {
                                this.getLinkHtml(item,index)
                            }
                        </TouchableOpacity>
                    );
                    info_html.push(roomClean_html);
                    break;
                case '查找遗留':
                    let searchMiss_html = (
                        <TouchableOpacity activeOpacity={0.8} onPress={()=>{
                            this.props.navigation.navigate('searchMissScreen',{room:_this.state.room,callBack:()=>_this.backScreen()});
                        }}>
                            {
                                this.getLinkHtml(item,index)
                            }
                        </TouchableOpacity>
                    );
                    info_html.push(searchMiss_html);
                    break;
                case '客人催服务':
                    let guestHurry_html = (
                        <TouchableOpacity activeOpacity={0.8} onPress={()=>{
                            this.props.navigation.navigate('guestServiceScreen',{room:_this.state.room,callBack:()=>_this.backScreen()});
                        }}>
                            {
                                this.getLinkHtml(item,index)
                            }
                        </TouchableOpacity>
                    );
                    info_html.push(guestHurry_html);
                    break;
            }

        });
        return info_html
    }

    getLinkHtml=(item,index)=>{
        return (
            <View key={index} >
                <View style={[styles.container]}>
                    {
                        (item.iconl)&&(
                            <View style={styles.iconBox}>
                                {
                                    this.getIcon(JSON.parse(item.iconl))
                                }
                            </View>
                        )
                    }

                    <View style={styles.textBox}>
                        <View style={styles.leftBox}>
                            <Text style={styles.text}>
                                {item.title}
                            </Text>
                        </View>
                    </View>
                    <View style={styles.arrowBox}>
                        <Text style={styles.infoText}>
                            {item.value}
                        </Text>
                    </View>
                    {
                        (item.iconr)&&(
                            <View style={{marginRight:0}}>
                                {
                                    this.getIcon(JSON.parse(item.iconr))
                                }
                            </View>
                        )
                    }
                    <View style={styles.arrowBox}>
                        <AntDesign name={'right'} size={18} style={{color: FONT_COLOR_SUB_TITLE}}/>
                    </View>
                </View>
                {
                    (item['summary']) && (
                        <View style={styles['summary']}>
                            <Text style={styles.summaryText}>
                                {item['summary']}
                            </Text>
                        </View>
                    )
                }

            </View>
        )
    };

    getIcon=(iconJson)=>{
        let icon_html;
        switch (iconJson.type) {
            case 'AntDesign':
                icon_html = (<AntDesign name={iconJson.name} size={20} style={{color: iconJson.tcolor}}/>);
                break;
            case 'FontAwesome':
                icon_html = (<FontAwesome name={iconJson.name} size={20} style={{color: iconJson.tcolor}}/>);
                break;
            case 'FontAwesome5':
                icon_html = (<FontAwesome5 name={iconJson.name} size={20} style={{color: iconJson.tcolor}}/>);
                break;
            case 'MaterialCommunityIcons':
                icon_html = (<MaterialCommunityIcons name={iconJson.name} size={20} style={{color: iconJson.tcolor}}/>);
                break;
            case 'MaterialIcons':
                icon_html = (<MaterialIcons name={iconJson.name} size={20} style={{color: iconJson.tcolor}}/>);
                break;
            case 'number':
                icon_html = (
                    <View style={[styles.rightBox]}>
                        <Text style={[styles.numText]}>
                            {iconJson.name}
                        </Text>
                    </View>
                );
                break;

        }
        return icon_html
    };

    freshScreen=()=>{
        let {dispatch} = this.props;

        let str = {"arg":{"orderid":this.state.orderid,"room":this.state.room}}
        dispatch(retrieveRoommates(JSON.stringify(str),this.state.procedure))
    };

    backScreen=()=>{
        let {dispatch} = this.props;

        this.props.navigation.navigate('orderInfo');

        let str = {"arg":{"orderid":this.state.orderid,"room":this.state.room}}
        dispatch(retrieveRoommates(JSON.stringify(str),this.state.procedure))
    };

    getTitle(Info){
        const { setParams } = this.props.navigation;
        let title = JSON.parse(Info);
        let title_left = (
            <View style={{flexDirection: 'row',marginLeft: -15,}}>
                <Text style={{fontSize:17,fontWeight: '100',color:FONT_COLOR_TITLE,marginTop:3}}>
                    {title.title}
                </Text>
                <Text style={{marginLeft:5,marginTop:3}}>
                    <FontAwesome name={'circle'} size={25} style={{color: title.bgcolor}}/>
                </Text>
            </View>
        );

        setParams({title_left:title_left});
    };

}

const props = state => {
    return {
        onRetrieveRoommates:state.onRetrieveRoommates,
    }
}

export default RoommatesScreen = connect(props)(RoommatesScreen);